package com.stone.serverless.auth.context;

import cn.dev33.satoken.context.SaTokenContext;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.servlet.model.SaRequestForServlet;
import cn.dev33.satoken.servlet.model.SaResponseForServlet;
import cn.dev33.satoken.servlet.model.SaStorageForServlet;
import com.stone.serverless.auth.matcher.SaPathMatcherHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Description: TODO
 * @Author: MECHREVO
 * @Date: 2022/1/27 8:47
 * @Version: 1.0.0
 */
public class StoneSaTokenContext implements SaTokenContext {
    @Override
    public SaRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return new SaRequestForServlet(servletRequestAttributes.getRequest());
    }

    @Override
    public SaResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
          return new SaResponseForServlet(servletRequestAttributes.getResponse());
    }

    @Override
    public SaStorage getStorage() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return new SaStorageForServlet(servletRequestAttributes.getRequest());
    }

    @Override
    public boolean matchPath(String pattern, String path) {
        return SaPathMatcherHolder.getPathMatcher().match(pattern, path);

    }


    @Override
    public boolean isValid() {
        return true;
    }
}
